<template>
  <div class="judge">
    <RealMonitor v-if="this.partensTitle == '实时监控'" />
    <BusinessManger v-if="this.partensTitle == '业务管理'" />
    <CarManger v-if="this.partensTitle == '车辆管理'" />
    <DriverManger v-if="this.partensTitle == '司机管理'" />
    <Equipment v-if="this.partensTitle == '设备管理'" />
    <DataQuery v-if="this.partensTitle == '数据查询'" />
    <!-- <Roles v-if="this.$route.params.title == '角色管理'" /> -->
    <Setting
      v-if="partensTitle == '功能配置'"
      :list="$route.params.list"
    />
    <DailyManger v-if="this.partensTitle == '管理日志'" />
    <CommonPage
      v-if="judgeShow()"
      :sort="sort"
      :button="buttonObject"
      :tableData="tableData"
      :nameData="nameData"
      :operating="operating"
      :operatWidth="operatWidth"
      :pagination="pagination"
      :currentChange="currentChange"
    />
    <el-dialog :visible.sync="ownVisible" width="80%">
      <div class="myDialog">
        <RoleEdit />
      </div>
    </el-dialog>
  </div>
</template> 

<script>
import CarManger from "./carManger";
import RealMonitor from "./realMonitor";
import Setting from "./setting";
import DailyManger from "./dailyManger";
import CommonPage from "@/components/commenPage";
import Equipment from "./equitpment";
import DataQuery from "./dataQuery";
import DriverManger from "./driverManger";
import BusinessManger from "./businessManger";
import Roles from "./roles";
import RoleEdit from "@/components/roleEdit";

export default {
  data() {
    return {
      ownVisible: false,
      partensTitle: "",
      tableData: [],
      nameData: [],
      buttonObject: {
        show: false,
        name: "添加",
        dialogVisible: false,
        confirm: () => {},
        click: () => {},
        topElement: [],
        myDialog: [],
        myDialogTitle: "合作伙伴添加",
      },
      sort: false,
      operating: [],
      operatWidth: "",
      pageNum: 1,
      pagination: 1,
    };
  },
  components: {
    RealMonitor,
    CarManger,
    Setting,
    DailyManger,
    CommonPage,
    Equipment,
    DataQuery,
    DriverManger,
    BusinessManger,
    Roles,
    RoleEdit,
  },
  props: {
    title: String,
    list: Array,
  },

  methods: {
    getData() {
      let date = new Date();
      return (
        date.getFullYear() +
        "年" +
        date.getMonth() +
        "月" +
        date.getDay() +
        "日"
      );
    },
    judgeShow() {
      let oldWay = [
        "司机管理",
        "实时监控",
        "车辆管理",
        "功能配置",
        "管理日志",
        "",
        "设备管理",
        "数据查询",
        "业务管理",
      ];
     
      return !oldWay.includes(this.partensTitle==undefined?'':this.partensTitle);
    },
    judgeContent() {
      switch (this.partensTitle) {
        case "合作伙伴":
          this.nameData = [
            {
              label: "伙伴名称",
              property: "friendName",
            },
            {
              label: "合作代码",
              property: "friendCode",
            },
            {
              label: "账号状态",
              property: "accountState",
            },
            {
              label: "角色",
              property: "character",
            },
            {
              label: "创建时间",
              property: "createTime",
            },
            {
              label: "有效期",
              property: "validPeriod",
            },
            {
              label: "手机号码",
              property: "phone",
            },
            {
              label: "姓名",
              property: "name",
            },
            {
              label: "邮箱地址",
              property: "emailAdress",
            },
            {
              label: "APP账号",
              property: "account",
            },
            {
              label: "PC账号数",
              property: "accountNum",
            },
            {
              label: "通讯地址",
              property: "communication",
            },
            {
              label: "备注",
              property: "commit",
            },
          ];
          this.tableData = [
            {
              commit: "asdas",
              communication: "sichuan",
              accountNum: "23",
              account: 1232131,
              emailAdress: "aq3232423@qq.com",
              name: "asdsad",
              phone: "232131233",
              validPeriod: "2999-02-02",
              createTime: "2020-02-02 12:00",
              character: "合作伙伴",
              accountState: "启用",
              friendCode: "we23",
              friendName: "dhkjsahkjd",
            },
          ];
          this.sort = true;
          this.operating = [
            {
              name: "编辑",
            },
            { name: "删除" },
            { name: "重置密码" },
          ];
          this.buttonObject.click = () => {
            this.buttonObject.dialogVisible = true;
          };
          this.buttonObject.myDialogTitle = "合作伙伴添加";
          this.buttonObject.myDialog = [
            { title: "所属系统代码：", value: "", type: "input", bind: "" },
            { title: "所属系统名称：", value: "", type: "input", bind: "" },
            {
              title: "合作代码：",
              value: "",
              type: "input",
              bind: "friendCode",
            },
            {
              title: "合作伙伴名称：",
              value: "",
              type: "input",
              bind: "friendName",
            },
            {
              title: "账号状态：",
              value: "",
              type: "radio",
              valueData: ["启用", "禁用"],
              bind: "accountState",
            },
            {
              title: "角色：",
              value: "",
              type: "input",
              bind: "character",
            },
            {
              title: "登录账号/手机：",
              value: "",
              type: "input",
              bind: "phone",
            },
            { title: "有效期：", value: "", type: "date", bind: "validPeriod" },
            { title: "负责人姓名：", value: "", type: "input", bind: "name" },
            { title: "邮箱：", value: "", type: "input", bind: "emailAdress" },
            { title: "APP账号数：", value: "", type: "input", bind: "account" },
            {
              title: "PC账号数：",
              value: "",
              type: "input",
              bind: "accountNum",
            },
            {
              title: "通讯地址：",
              value: "",
              type: "input",
              bind: "communication",
            },
            { title: "备注：", value: "", type: "input", bind: "commit" },
          ];

          this.buttonObject.confirm = () => {
            let next = this.buttonObject.myDialog.some((ele, i) => {
              if (ele.value == "") {
                next = false;
                this.$message({
                  message: `请输入${ele.title}`,
                  offset: 300,
                });
                return true;
              }
            });
            if (!next) {
              let objectTemp = {};
              this.buttonObject.myDialog.filter((ele, i) => {
                if (ele.bind != "") {
                  objectTemp[ele.bind] = ele.value;
                }
              });
              let date = new Date();
              objectTemp["createTime"] =
                date.getFullYear() +
                "年" +
                date.getMonth() +
                "月" +
                date.getDay() +
                "日";

              this.tableData.push(objectTemp);
              this.buttonObject.dialogVisible = false;
            }
          };
          this.operatWidth = "200";
          this.buttonObject.show = true;
          break;
        case "角色管理":
          this.sort = true;

          this.nameData = [
            {
              label: "角色名",
              property: "name",
            },
            {
              label: "所属企业",
              property: "enterprise",
            },
            {
              label: "创建时间",
              property: "createTime",
            },
          ];
          this.tableData = [
            // {
            //   name: "超级管理员",
            //   enterprise: "成都科技有限公司",
            //   Authority: "配置",
            //   createTime: "2019-12-12 12:00",
            // },
          ];
          this.assginGetData = () => {
            this.axios("post", this.inter.postRoleList, {
              pageNum: this.pageNum,
              pageSize: 10,
            }).then((res) => {
              this.tableData = [];
              res.list.filter((item) => {
                this.tableData.push({
                  name: item.roleName,
                  enterprise: item.status,
                  createTime: item.createTime,
                });
              });
            });
          };
          this.assginGetData();
          this.operatWidth = "130";
          this.operating = [
            {
              name: "编辑",
              fun: (index) => {
                this.ownVisible = true;
              },
            },
            {
              name: "配置",
              fun: (index) => {},
            },
            { name: "删除", fun: (index) => {} },
          ];
          this.buttonObject.show = true;
          this.buttonObject.myDialogTitle = "角色添加";
          this.buttonObject.myDialog = [
            { title: "角色名：", value: "", type: "input", bind: "roleName" },
            { title: "角色排序", value: "", type: "input", bind: "roleSort" },
          ];
          this.buttonObject.click = () => {
            this.buttonObject.dialogVisible = true;
          };
          this.buttonObject.confirm = () => {
            let temp = {};
            let next = this.buttonObject.myDialog.some((ele, i) => {
              if (ele.value == "") {
                this.$message({
                  message: `请输入${ele.value}`,
                  offset: 300,
                });
                return ture;
              } else {
                temp[ele.bind] = ele.value;
              }
            });

            if (!next) {
              // this.tableData.push(temp);
              this.axios("post", this.inter.postAddRole, temp).then((res) => {
                this.assginGetData();
              });
            }
          };

          break;
        case "账户管理":
          let userId = 0;
          this.sort = true;
          this.operatWidth = "200";
          this.operating = [
            {
              name: "编辑",
              fun: (index) => {
                this.buttonObject.dialogVisible = true;
                isEdit = true;
                this.buttonObject.myDialog[0]["value"] = this.tableData[index][
                  "companyId"
                ];
                this.buttonObject.myDialog[1]["value"] = this.tableData[index][
                  "company"
                ];
                this.buttonObject.myDialog[2]["value"] =
                  this.tableData[index]["state"] === "0" ? "启用" : "禁用";
                this.buttonObject.myDialog[4]["value"] = this.tableData[index][
                  "account"
                ];
                this.buttonObject.myDialog[5]["value"] = this.tableData[index][
                  "name"
                ];
                this.buttonObject.myDialog[6]["value"] = this.tableData[index][
                  "email"
                ];
                this.buttonObject.myDialog[8]["value"] = this.tableData[index][
                  "commit"
                ];
                userId = this.tableData[index]["userId"];
              },
            },
            {
              name: "删除",
              fun: (index) => {
                this.axios(
                  "post",
                  this.inter.postRemoveUser +
                    `?ids=${this.tableData[index]["userId"]}`
                ).then((res) => {
                  this.assginGetData();
                });
              },
            },
            {
              name: "重置密码",
              fun: (index) => {
                this.axios(
                  "post",
                  this.inter.postResetPsd + this.tableData[index]["userId"]
                ).then((res) => {
                  this.$message({
                    message: "密码重置为手机号后6位",
                    offset: 300,
                  });
                });
              },
            },
          ];
          this.nameData = [
            { label: "所属企业ID", property: "companyId" },
            { label: "所属企业", property: "company" },
            { label: "账号状态", property: "state" },
            { label: "用户账号", property: "account" },
            { label: "用户姓名", property: "name" },
            { label: "邮箱地址", property: "email" },
            { label: "备注", property: "commit" },
          ];
          this.tableData = [];
          this.buttonObject.show = true;
          this.buttonObject.click = () => {
            isEdit = false;
            this.buttonObject.dialogVisible = true;
          };
          let isEdit = false;
          this.assginGetData = () => {
            this.tableData = [];
            this.axios(
              "post",
              this.inter.postUserList +
                `?pageNum=${this.pageNum}&pageSize=10&type=0`
            ).then((res) => {
              res.list.filter((item) => {
                this.tableData.push({
                  userId: item.userId,
                  companyId: item.companyId,
                  company: item.companyName,
                  state: item.status,
                  account: item.phone,
                  name: item.nickname,
                  email: item.email,
                  commit: item.remark,
                });
              });
              this.pagination = res.total;
            });
          };

          this.assginGetData();

          this.buttonObject.myDialogTitle = "账户添加";
          this.buttonObject.myDialog = [
            {
              title: "所属代码：",
              value: "",
              type: "input",
              bind: "cooperateId",
            },
            {
              title: "公司名称：",
              value: "",
              type: "input",
              bind: "cooperateName",
            },
            {
              title: "账号状态：",
              value: "",
              type: "radio",
              bind: "status",
              valueData: ["启用", "禁用"],
            },
            {
              title: "角色：",
              value: "",
              type: "select",
              valueData: [
                { label: "普通账户", value: 0 },
                { label: "公司合伙人", value: 1 },
              ],
              bind: "roleId",
            },
            {
              title: "登录账号/手机：",
              value: "",
              type: "input",

              bind: "phone",
            },
            {
              title: "姓名：",
              value: "",
              type: "input",
              bind: "nickname",
            },
            {
              title: "邮箱：",
              value: "",
              type: "input",
              bind: "email",
            },
            {
              title: "通讯地址：",
              value: "",
              type: "input",
              bind: "address",
            },
            { title: "备注：", value: "", type: "input", bind: "remark" },
          ];
          this.buttonObject.confirm = () => {
            let _object = {};
            let next = this.buttonObject.myDialog.some((item) => {
              if (item.value === "") {
                this.$message({
                  message: "请填写" + item.title,
                  offset: 300,
                });
                return true;
              } else {
                _object[item.bind] = item.value;
                if (item.title === "账号状态：") {
                  _object[item.bind] = item.value === "启用" ? 0 : 1;
                }
              }
            });

            _object.validity = " ";
            _object.type = 0;
            _object.validity = " ";
            _object.appNum = 0;
            if (!next) {
              if (isEdit) {
                this.axios("post", this.inter.postAmendUser + userId).then(
                  (res) => {
                    this.buttonObject.dialogVisible = false;
                    this.assginGetData();
                  }
                );
              } else {
                this.axios("post", this.inter.postUserAdd, _object).then(
                  (res) => {
                    this.buttonObject.dialogVisible = false;
                    this.assginGetData();
                  }
                );
              }
            }
          };

          break;
        default:
          this.tableData = [];
          this.nameData = [];
          this.operating = [];
          this.sort = false;
          this.buttonObject.show = false;
          this.buttonObject.topElement = [];
          this.assginGetData = () => {};
          break;
      }
    },
    currentChange(page) {},
    assginGetData() {},
  },
  computed: {
    getTitle() {
      return this.$route.path.split('/');
    },
  },
  watch: {
    getTitle(news, old) {
      if(news.length>2){
         this.partensTitle = news[2];
      }
      this.judgeContent();
      this.judgeShow();
    },
  },
  mounted(){
    let initRouter =  this.$route.path.split('/')
    if(initRouter.length>2){
         this.partensTitle = initRouter[2];
      }

  }
};
</script>

<style>
.judge {
  /* width: 100%; */
  height: 100%;
}
</style>